﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;

namespace CNKI.Graduate.Framework.Cache
{
    public class RedisClient
    {
        private static ConnectionMultiplexer _redis;
        private static readonly object Locker = new object();

        // static ConfigurationOptions _configurationOptions = ConfigurationOptions.Parse("192.168.25.83" + ":" + "6379");

        public static ConnectionMultiplexer Manager
        {
            get
            {
                if (_redis == null)
                {
                    lock (Locker)
                    {
                        if (_redis != null) return _redis;

                        _redis = GetManager();
                        return _redis;
                    }
                }

                return _redis;
            }
        }

        private static ConnectionMultiplexer GetManager(string connectionString = null)
        {
            if (string.IsNullOrEmpty(connectionString))
            {
                var redisServer = ConfigurationManager.AppSettings["RedisServer"];
                var redisServerPwd = ConfigurationManager.AppSettings["RedisServerPwd"];
                connectionString = $"{redisServer},password={redisServerPwd}";

            }

            return ConnectionMultiplexer.Connect(connectionString);
        }
    }
}
